package cn.play.cardgame.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.play.cardgame.R;
import cn.play.cardgame.constant.Constant;
import cn.play.cardgame.event.CallbackEvent;
import cn.play.cardgame.util.ImageUtil;
import de.greenrobot.event.EventBus;

public class FlowBottomInfoPanel extends FrameLayout {
	@InjectView(R.id.convert_bet_min_score)
	ImageView mConvertBetImg;
	private Context mContext;
	private int DEFAULT_BET_POS = 0;
	private int[] converts = { 5, 10, 100, 500 };

	public FlowBottomInfoPanel(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		initViews();
	}

	public FlowBottomInfoPanel(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public FlowBottomInfoPanel(Context context) {
		this(context, null);
	}

	private void initViews() {
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);

		View view = View.inflate(mContext, R.layout.flow_bottom_info_panel, null);
		ButterKnife.inject(this, view);
		setBetMin(converts[DEFAULT_BET_POS]);
		this.addView(view, params);
	}

//	@Override
//	public boolean onKeyUp(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_5) {
//			DEFAULT_BET_POS = DEFAULT_BET_POS + 1;
//			if (DEFAULT_BET_POS >= converts.length) {
//				DEFAULT_BET_POS = 0;
//			}
//			setBetMin(converts[DEFAULT_BET_POS]);
//			EventBus.getDefault().post(new CallbackEvent(Constant.EVENT_NOTIFY_CHANGE_BET_STEP,
//					String.valueOf(converts[DEFAULT_BET_POS])));
//		}
//		return super.onKeyUp(keyCode, event);
//	}
	public void setBetStep(){
		DEFAULT_BET_POS = DEFAULT_BET_POS + 1;
		if (DEFAULT_BET_POS >= converts.length) {
			DEFAULT_BET_POS = 0;
		}
		setBetMin(converts[DEFAULT_BET_POS]);
		EventBus.getDefault().post(
				new CallbackEvent(Constant.EVENT_NOTIFY_CHANGE_BET_STEP, String.valueOf(converts[DEFAULT_BET_POS])));
	}

	@OnClick(R.id.btn_convert_bet_min_score)
	public void clickConvertBet(View view) {
		setBetStep();
	}
	
	@SuppressWarnings("deprecation")
	private void setBetMin(int betMin) {
		mConvertBetImg.setBackgroundDrawable(ImageUtil.getInstance().bitmapToDrawableWithGap(mContext,
				ImageUtil.getConvertMapping(), String.format("%04d", betMin), 10));
	}
}
